package com.leyou.item.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.leyou.common.exception.LyException;
import com.leyou.item.dto.SpecGroupDTO;
import com.leyou.item.dto.SpecParamDTO;
import com.leyou.item.entity.SpecGroup;
import com.leyou.item.entity.SpecParam;
import com.leyou.item.mapper.SpecGroupMapper;
import com.leyou.item.service.SpecGroupService;
import com.leyou.item.service.SpecParamService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Service
public class SpecGroupServiceImpl extends ServiceImpl<SpecGroupMapper, SpecGroup> implements SpecGroupService {

    @Autowired
    private SpecParamService specParamService;

    @Override
    public List<SpecGroupDTO> queryGroupByCategoryId(Long cid) {

        List<SpecGroup> specGroups = this.query().eq("category_id", cid).list();

        return SpecGroupDTO.convertEntityList(specGroups);
    }

    @Override
    public void saveGroup(SpecGroupDTO specGroupDTO) {

        this.save(specGroupDTO.toEntity(SpecGroup.class));

    }

    @Override
    public void updateGroup(SpecGroupDTO specGroupDTO) {

        this.updateById(specGroupDTO.toEntity(SpecGroup.class));

    }

    @Override
    public void deleteGroup(Long id) {

        this.removeById(id);

    }

    @Override
    public List<SpecGroupDTO> querySpecList(Long cid) {

        List<SpecGroupDTO> groupDTOList = SpecGroupDTO.convertEntityList(this.query().eq("category_id", cid).list());

        if (CollectionUtils.isEmpty(groupDTOList)){

            throw new LyException(HttpStatus.NOT_FOUND.value(),"该分类下没有规格组参数信息！");

        }

        List<SpecParamDTO> paramDTOList = specParamService.querySpecParams(cid, null, null);

        // 对SpecParamDTO的集合进行分组，按照groupId进行分组处理，map的key就是groupId，value就是此groupId对应的规格参数集合
        Map<Long, List<SpecParamDTO>> map = paramDTOList.stream().collect(Collectors.groupingBy(SpecParamDTO::getGroupId));

        // 循环给每个group赋值对应的param信息
        for (SpecGroupDTO specGroupDTO : groupDTOList) {

//            List<SpecParam> group_id = specParamService.query().eq("group_id", specGroupDTO.getId()).list();
//
//            group_id.stream().collect(Collectors.groupingBy(SpecParam::getGroupId));
//
//            specGroupDTO.setParams(SpecParamDTO.convertEntityList(group_id));

            specGroupDTO.setParams(map.get(specGroupDTO.getId()));

        }

        // 这种方式不好用，首先循环会根据 specGroupDTO 的数量走多次，并且其中的类型转换也会走多次 效率较低
//        groupDTOList.forEach(specGroupDTO ->
//                specGroupDTO.setParams(SpecParamDTO.convertEntityList(
//                        this.specParamService.query().eq("group_id",specGroupDTO.getId()).list())));


        return groupDTOList;

    }

}
